package com.thinta.wind.ui.home.fragment

import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import com.thinta.wind.R
import com.thinta.wind.base.BaseFragment
import com.thinta.wind.databinding.DailyFragmentBinding
import com.thinta.wind.interfaces.OnItemClickListener
import com.thinta.wind.ui.home.adapter.DailyAdapter
import com.thinta.wind.ui.home.repository.DailyRepository
import com.thinta.wind.ui.home.viewmodel.DailyModelFactory
import com.thinta.wind.ui.home.viewmodel.DailyViewModel

class DailyFragment : BaseFragment<DailyViewModel, DailyFragmentBinding>(){

    companion object {
        fun newInstance() = DailyFragment()
    }

    private val mDailyAdapter : DailyAdapter by lazy { DailyAdapter() }

    override fun viewModelClass() = DailyViewModel::class.java

    override fun getLayoutId() = R.layout.daily_fragment

    override fun afterCreate() {
        initData()
        dataBinding?.dailyList?.layoutManager = LinearLayoutManager(context)
        dataBinding?.dailyList?.isNestedScrollingEnabled = true
        (dataBinding?.dailyList?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        dataBinding?.dailyList?.adapter = mDailyAdapter
        initListener()
    }

    private fun initData(){
        viewModel?.fetchResult()
        viewModel?.result?.observe(this, {
            mDailyAdapter.submitList(it)
        })
    }

    private fun initListener(){
        mDailyAdapter.setOnItemClickListener(OnItemClickListener{position, view ->

        })
    }

    override fun getModelFactory(): ViewModelProvider.NewInstanceFactory = DailyModelFactory(
        DailyRepository()
    )

}
